<template>
  <div
    v-if="!!info"
    :class="{ 'virtual-coin': true, 'is-disabled': info.disabled }"
  >
    <van-cell :clickable="!info.disabled" @click="onChange(info.pay_way)">
      <template #title>
        <img :src="info.icon" class="virtual-coin__icon" />
        <span>余额</span>
        <span class="price theme-customize-font">{{ balanceCount }}</span>
        <span class="price-text">{{ info.name }}(¥{{ balanceCount }})</span>
      </template>
      <template #right-icon>
        <div
          class="btn inline theme-customize-bg"
          v-if="info.isInsufficientBalance"
          @click="recharge"
        >
          充值
        </div>
        <van-radio v-else :name="info.pay_way" :disabled="info.disabled" />
      </template>
    </van-cell>
    <div class="paydetail info" v-if="info.isInsufficientBalance">余额不足</div>
    <!-- todo 待完善 -->
    <div class="virtual-text" v-if="info.showLimitTips">
      因iOS限制，仅支持{{ info.name }}支付
    </div>
  </div>
</template>

<script>
import { Radio, Cell } from "vant";
import { toFixedPrice } from "../../../utils/price";

export default {
  name: "VirtualIcon",
  components: {
    [Cell.name]: Cell,
    [Radio.name]: Radio,
  },
  props: {
    value: null,
    info: {
      type: Object,
      default: () => ({}),
    },
  },
  mounted() {
    let that = this;
    if (window.wx?.noticeOrderH5Refresh) {
      window.wx?.noticeOrderH5Refresh({
        success: (data) => {
          that.$emit("refreshBalance");
        },
      });
    }
  },
  computed: {
    balanceCount() {
      return toFixedPrice(this.info?.balance || 0);
    },
  },
  methods: {
    onChange() {
      if (!this.isInsufficientBalance && !this.info.disabled) {
        this.$emit("change", this.info.pay_way);
      }
    },
    recharge() {
      if (window.wx && window.wx.nativeIAPRechargeByAlert) {
        window.wx.nativeIAPRechargeByAlert({});
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.inline {
  display: inline-block;
}
.virtual-coin {
  // padding: 0.32rem 0.32rem 0;
  background: #fff;
  margin-top: 0.16rem;
  position: relative;

  ::v-deep .van-cell {
    &__title {
      display: flex !important;
      align-items: center;
    }

    .btn {
      width: 1.6rem;
      height: 0.56rem;
      line-height: 0.56rem;
      text-align: center;
      color: #ffffff;
      border-radius: 0.4rem;
      font-size: 0.24rem;
      background-color: #4fa2f3;
    }

    .price {
      color: #4fa2f3;
    }
  }

  &__icon {
    width: 0.48rem;
    height: 0.48rem;
    margin-right: 0.16rem;
  }

  .price-text {
    color: #666666;
    margin-left: 0.14rem;
  }
  .paydetail-price {
    margin-left: 0.16rem;
  }
  .price {
    margin-left: 0.08rem;
  }

  .font {
    font-size: 0.28rem;
    color: #333333;
    font-weight: 500;
  }
  .paydetail {
    color: #999999;
    margin-left: 0.56rem;
    font-size: 0.24rem;
    padding-bottom: 0.32rem;
  }
  .bor-top {
    border-top: 0.5px solid #ccc;
  }
  .paydetail-box {
    height: 1.12rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 0.32rem;
    .btn {
      width: 1.6rem;
      height: 0.56rem;
      line-height: 0.56rem;
      text-align: center;
      color: #ffffff;
      border-radius: 0.4rem;
      font-size: 0.24rem;
    }
    .paydetail-info {
      font-size: 0.28rem;
      display: flex;
      align-items: center;
      img {
        width: 0.4rem;
        height: 0.4rem;
        vertical-align: middle;
      }
      .price-text {
        color: #666666;
        margin-left: 0.14rem;
      }
      .paydetail-price {
        margin-left: 0.16rem;
      }
      .price {
        margin-left: 0.08rem;
      }
    }
  }
}
.virtual-text {
  padding: 0.16rem 0.32rem;
  color: #999999;
  font-size: 0.24rem;
}
</style>
